package com.jinlejia.erp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemAppointmentVisitBinding;
import com.jinlejia.erp.utils.DateUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 约看（adapter）
 */

public class AppointmentVisitAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    private String sex;
    private HashMap<String,Integer> dateMap;

    public AppointmentVisitAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
       dateMap = new HashMap<>();
    }

    public AppointmentVisitAdapter(ArrayList<Map<String, Object>> data, String sex) {
        super(data);
        this.sex = sex;
        dateMap = new HashMap<>();
    }
    public void setData(ArrayList<Map<String, Object>> data){
        super.setData(data);

    }
    public void addData(ArrayList<Map<String, Object>> data){
        super.addData(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_appointment_visit, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemAppointmentVisitBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        Map<String, Object> map = mData.get(position);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

        if (map.containsKey("state") && null != map.get("state")) {//状态
            binding.appointmentState.setTextColor(context.getResources().getColor(R.color.red));
            switch (map.get("state").toString()) {
                case "0":
                    binding.appointmentState.setText("未完成");
                    break;
                case "1":
                    binding.appointmentState.setText("已完成");
                    binding.appointmentState.setTextColor(context.getResources().getColor(R.color.green));
                    break;
                case "2":
                    binding.appointmentState.setText("已取消");
                    break;
                case "3":
                    binding.appointmentState.setText("已过期");
                    break;
            }
        }
        if (map.containsKey("sex") && !TextUtils.isEmpty(map.get("sex").toString())) {
            sex = map.get("sex").toString();
        }
        if (map.containsKey("customerName")) { //客户姓名
            if (sex.equals("1")) {
                binding.appointmentName.setText(map.get("customerName").toString().concat(" 先生"));
            } else {
                binding.appointmentName.setText(map.get("customerName").toString().concat(" 女士"));
            }
        }

        if (map.containsKey("firstEstateName"))  //小区名
            binding.appointmentHouse.setText(map.get("firstEstateName").toString().concat("等"));

        if (map.containsKey("countHouse"))
            binding.houseNumber.setText(map.get("countHouse").toString());

        if (map.containsKey("appointmentStartTime") && map.containsKey("appointmentEndTime"))  //开始时间 和结束时间
            binding.appointmentTime.setText(sdf.format(new Date(Long.valueOf(map.get("appointmentStartTime").toString()))) + " - " +
                    sdf.format(new Date(Long.valueOf(map.get("appointmentEndTime").toString()))));

        String date = DateUtil.getTime(map.get("appointmentStartTime").toString(), "yyyy-MM-dd");
        if (dateMap.containsKey(date) && dateMap.get(date) != position ) { //日期
            //不是第一次加载该日期item
            binding.appointmentDay.setVisibility(View.GONE);
        } else if(dateMap.containsKey(date) && dateMap.get(date) == position  ) {
            //已经加载过，当前position为该日期第一项
            binding.appointmentDay.setVisibility(View.VISIBLE);
            binding.appointmentDay.setText(date);
        }else if( !dateMap.containsKey(date) ){
            //第一次加载该日期item
            dateMap.put(date,position);
            binding.appointmentDay.setVisibility(View.VISIBLE);
            binding.appointmentDay.setText(date);
        }

        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }
}
